import 'package:flutter/material.dart';
import 'package:flutter/src/foundation/key.dart';
import 'package:flutter/src/widgets/framework.dart';

class DataTimeRangerDemo extends StatefulWidget {
  const DataTimeRangerDemo({Key? key}) : super(key: key);

  @override
  State<DataTimeRangerDemo> createState() => _DataTimeRangerDemoState();
}

class _DataTimeRangerDemoState extends State<DataTimeRangerDemo> {

// 不要使用late File _image;，而要使用像 File? _image;这样的可空文件
 String? _dataSelectText;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("日期范围选择"),
      ),
      body: Center(
        child: Text("当前选择$_dataSelectText"),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: (){
          showDateSelect();
        },
        child: Icon(Icons.select_all),
      ),

    );
    
  }

  void showDateSelect() async{
    DateTime start = DateTime.now();
    DateTime end = DateTime(start.year,start.month,start.day + 4);
    DateTimeRange? selectTimeRange = await showDateRangePicker(
      context: context, 
      // 开始时间
      firstDate: DateTime(2022,1), 
      // 结束时间
      lastDate: DateTime(2022,12),
      cancelText: "取消",
      confirmText: "确定",
      initialDateRange: DateTimeRange(start: start, end: end)
      );

       _dataSelectText = selectTimeRange.toString();
      setState(() {
       
        DateTime selectStart = selectTimeRange!.start;
        DateTime selectEnd = selectTimeRange.end;
      });
      
  }
}